package com.wanmait.wanmarecruit.webController;

import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.entity.Notice;
import com.wanmait.wanmarecruit.entity.User;
import com.wanmait.wanmarecruit.service.ConfigService;
import com.wanmait.wanmarecruit.service.NoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("web")
public class WebNoticeController {
    @Resource
    private NoticeService noticeService;
    @Resource
    private ConfigService configService;

    //转发userNotice界面
    @RequestMapping("/personal/userNotice")
    public ModelAndView userNotice(HttpSession session)
    {
        Integer userId = ((User)session.getAttribute("user")).getId();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("notices",noticeService.selectAllNoticeByUser(userId,1,10));
        modelAndView.addObject("numOfNotice",noticeService.getAllNoticeNotRead(userId));
        return modelAndView;
    }

    @RequestMapping("/personal/userNoticeTable")
    @ModelAttribute("notices")
    public ModelAndView userNoticeTable(HttpSession session,Integer pageNum)
    {
        Integer userId = ((User)session.getAttribute("user")).getId();
        Integer pageSize = Integer.parseInt(configService.getByKeyword("notice_paging_size").getConfigValue());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("notices",noticeService.selectAllNoticeByUser(userId,pageNum,pageSize));
        return modelAndView;
    }

    @RequestMapping("/personal/deleteNotice")
    @ResponseBody
    public void deleteNotice(Integer id)
    {
        noticeService.removeById(id);
    }

    @RequestMapping("/personal/noticeIsReading")
    @ResponseBody
    public void noticeIsReading(Integer id)
    {
        noticeService.updateNoticeIsReadingById(id);
    }

    @GetMapping("/personal/batchDeleteNotice")
    @ResponseBody
    public void batchDeleteNotice(Integer[] ids)
    {
        noticeService.deleteBatchByIds(ids);
    }

    @GetMapping("/personal/batchNoticeIsReading")
    @ResponseBody
    public void batchNoticeIsReading(Integer[] ids)
    {
        noticeService.updateBatchIsReading(ids);
    }
}
